import torch
from torch.autograd import Variable
import os
import random
import linecache
import numpy as np
import torchvision
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms
from train import SiameseNetwork
from PIL import Image
import PIL.ImageOps
import matplotlib.pyplot as plt
import torch.nn.functional as F
import cv2
 
transform=transforms.Compose(
        [transforms.Resize((100, 100)), transforms.ToTensor()])
'''
model = torch.load('model.pth').cuda()
model.eval()
'''
model = torch.load('model.pth', map_location=torch.device('cpu'))
model.eval()

'''  实际图片测试  01'''
img1 = PIL.Image.open(r'D:\luanshengNT\att_faces\s55\4.jpg')
img2 = PIL.Image.open(r'D:\luanshengNT\att_faces\s55\3.jpg')

'''
02
img1 = PIL.Image.open(r'D:\luanshengNT\att_faces\s55\3.jpg')
img2 = PIL.Image.open(r'D:\luanshengNT\att_faces\s55\4.jpg')


正反例测试
'''
'''  测试组1
img1 = PIL.Image.open(r'D:\luanshengNT\att_faces\s22\1.pgm')
img2 = PIL.Image.open(r'D:\luanshengNT\att_faces\s7\9.pgm') 
'''
'''
img1 = PIL.Image.open(r'D:\luanshengNT\att_faces\s8\1.pgm')
img2 = PIL.Image.open(r'D:\luanshengNT\att_faces\s8\4.pgm')
'''
img1 = img1.convert("L")
img2 = img2.convert("L")
 
img11 = transform(img1)
img22 = transform(img2)
 
imgs1 = np.array(img11)
imgs1 = imgs1[0,...]
imgs2 = np.array(img22)
imgs2 = imgs2[0,...]
print(imgs1.shape)
 
input1 = img11.unsqueeze(0)
input2 = img22.unsqueeze(0)
''' 
output1, output2 = model(Variable(input1).cuda(), Variable(input2).cuda())


euclidean_distance = F.pairwise_distance(output1, output2)
#plt.imshow
diff = euclidean_distance.cpu().detach().numpy()[0]
print(euclidean_distance.cpu().detach().numpy()[0])
'''


output1, output2 = model(Variable(input1), Variable(input2))
# 计算欧氏距离
euclidean_distance = F.pairwise_distance(output1, output2)

# 可视化
diff = euclidean_distance.detach().numpy()[0]
print(euclidean_distance.detach().numpy()[0])
 
plt.subplot(1, 2, 1)
plt.title('diff='+str(diff))
plt.imshow(imgs1)
plt.subplot(1, 2, 2)
plt.imshow(imgs2)
 
plt.show()
